﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class AdminEnvironment : Form
	{
		private Data data = null;
		EditMode mode = EditMode.EditMode;

		public AdminEnvironment(Data data)
		{
			InitializeComponent();
			this.data = data;
			
		}

		private void AdminEnvironment_Load(object sender, EventArgs e)
		{
			lstEnvironments.ValueMember = "Value";
			lstEnvironments.DisplayMember = "Text";

			DataTable dt = data.GetEnvironments();
			FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstEnvironments);
			ResetListSelection();
			EnableInput(false);
		}

		private void FillList(DataTable table, int valueIndex, int textIndex, ListBox list)
		{
			if (table != null && table.Rows.Count > 0)
			{
				list.Items.Clear();

				for (int i = 0; i < table.Rows.Count; i++)
				{
					ComboItem ci = new ComboItem();
					ci.Text = table.Rows[i][textIndex].ToString();
					ci.Value = table.Rows[i][valueIndex].ToString();

					list.Items.Add(ci);
				}
			}
		}

		private void lstEnvironments_SelectedIndexChanged(object sender, EventArgs e)
		{
			LoadSelectedItem();
		}

		private void LoadSelectedItem()
		{
			ComboItem ci = (ComboItem)lstEnvironments.SelectedItem;
			DataTable table = data.GetEnvironmentByID(ci.Value);

			if (table != null && table.Rows.Count > 0)
			{
				txtDescription.Text = table.Rows[0]["Description"].ToString();
			}
		}

		private void EnableInput(bool enable)
		{
			txtDescription.Enabled = enable;
			btnSave.Enabled = enable;
			btnCancel.Enabled = enable;

			if (IsPermitted(btnEdit, data.User.Environments)) { btnEdit.Enabled = !enable; }
			else { btnEdit.Enabled = false; }
			if (IsPermitted(btnAdd, data.User.Environments)) { btnAdd.Enabled = !enable; }
			else { btnAdd.Enabled = false; }
			if (IsPermitted(btnRemove, data.User.Environments)) { btnRemove.Enabled = !enable; }
			else { btnRemove.Enabled = false; }
			lstEnvironments.Enabled = !enable;
		}

		private bool IsPermitted(Button btn, Permissions perm)
		{
			if (btn == btnRemove) { return perm.Delete; }
			if (btn == btnAdd) { return perm.Create; }
			if (btn == btnEdit) { return perm.Edit; }

			return false;
		}

		private void ResetListSelection()
		{
			if (lstEnvironments.Items.Count > 0)
			{
				lstEnvironments.SelectedIndex = 0;
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (lstEnvironments.SelectedItem != null)
			{
				EnableInput(true);
				txtDescription.Focus();
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
            if (!ValidateInput()) { return; }

			ComboItem ci = (ComboItem)lstEnvironments.SelectedItem;
			DataTable table = new DataTable();

			table.Columns.Add("Description");

			string[] row = new string[1];
			row[0] = txtDescription.Text;

			table.Rows.Add(row);

			if (mode == EditMode.EditMode)
			{
				data.UpdateEnvironmentByID(table, ci.Value);
			}
			else
			{	//create mode
				data.InsertEnvironment(table);
				//reset mode back to default
				mode = EditMode.EditMode;
			}

			DataTable dt = data.GetEnvironments();
			FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstEnvironments);
			ResetListSelection();
			EnableInput(false);
		}

        private bool ValidateInput()
        {
            string requiredFields = String.Empty;

            if (String.IsNullOrEmpty(txtDescription.Text)) { requiredFields += "Description\n"; }

            if (!String.IsNullOrEmpty(requiredFields))
            {
                MessageBox.Show("The following required fields are empty\n" + requiredFields, "Error");
                return false;
            }

            return true;
        }

		private void btnCancel_Click(object sender, EventArgs e)
		{
			LoadSelectedItem();
			mode = EditMode.EditMode;	//reset to default
			ResetListSelection();
			EnableInput(false);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			mode = EditMode.CreateMode;
			txtDescription.Text = String.Empty;
			EnableInput(true);
			txtDescription.Focus();
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			if (lstEnvironments.SelectedItem != null)
			{
				ComboItem ci = (ComboItem)lstEnvironments.SelectedItem;
				DialogResult res = MessageBox.Show("Are you sure you want to delete the item: " + ci.Text, "Confirm Deletion", MessageBoxButtons.YesNo);

				if (res == DialogResult.Yes)
				{
					data.DeleteEnvironmentByID(ci.Value);
					DataTable dt = data.GetEnvironments();
					FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstEnvironments);
					ResetListSelection();
				}
			}
		}
	}
}
